<?php

use Spay\Spay;
use Spay\Order;
use Spay\Exception\SpayException;

error_reporting(E_ERROR);
//生成支付订单
define("BASE_PATH", dirname(__FILE__));
require BASE_PATH . DIRECTORY_SEPARATOR . './config.php';

require BASE_PATH . DIRECTORY_SEPARATOR . '../vendor/autoload.php';

Spay::$appId = APP_ID;// 设置 API Key
Spay::$appSecretKey = APP_SECRET_KEY;

$amount = rand(1,200);//金额人民币精确到分
$channel = 'AlipayWap';
$channel = rand(0,1) ? 'AlipayWap' : 'Alipay';// 支付渠道
//$channel = 'WeixinPayQr';// 支付渠道
$order_no = date('Ymdhis',time())  . uniqid('T') ;//购买订单号

$extra = [];

switch ($channel) {
	case 'AlipayWap':
		$extra = [
				'success_url' => 'http://10.1.223.224:8082/alipaywapredirect.php'
			];
		break;
	case 'WeixinPayPub'://微信公证号支付
        $extra = [
            'open_id' => 'openidxxxxxxxxxxxx'// 用户在商户微信公众号下的唯一标识
        ];
        break;
}
$passback_params = "passback_params";
try{
	$ch = Order::create(array_merge([
			'subject' => '红色iPhone7 128G',
			'body' => 'red apple made in China',
			'amount' => $amount,
			'order_no' => $order_no,
            'currency'  => 'cny',
            'channel'   => $channel,// 支付使用的第三方支付渠道取值，请参考：https://www.pingxx.com/api#api-c-new
            'client_ip' => $_SERVER['REMOTE_ADDR'],// 发起支付请求客户端的 IP 地址，格式为 IPV4，如: 127.0.0.1
            'appid'       => APP_ID,
            //'passback_params' => $passback_params, //附件需要返回的参数
            'notify_url' => 'http://10.1.223.224:8082/notify.php'
		], $extra));
	if(defined('AlipayWap')){
		header('Content-Type:text/html;charset=utf-8');
	}
	else
	{
		header('Content-Type:application/json;charset=utf-8');
		echo $ch;
	}
	//print_r(json_decode($ch, true));
}catch(SpayException $se)
{
	echo $se->getMessage();
}